package com.jine.agent.web;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebFilter(filterName="myFilter",urlPatterns="/*")
public class MyFilter implements Filter {
    Logger logger = LoggerFactory.getLogger(MyFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info(">>>>>>>>>>>MyFilter>init>>>>>>>>>>>>>>>>");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        logger.info(">>>>>>>>>>>MyFilter>doFilter>>>>>>>>>>>>>>>>");
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponseWrapper httpResponse = new HttpServletResponseWrapper((HttpServletResponse) response);
        String path=httpRequest.getRequestURI();
        request.setAttribute("_uri",path);
        System.out.println("URI:"+path);
//        httpRequest.getRequestDispatcher("/").forward(request,response);
        // chain.doFilter(request,response);
        Map<String,java.lang.String[]> map =  request.getParameterMap();
        Map result = new HashMap();
        result.put("URI",path);
        result.put("Param",map);
        response.getWriter().print(JSON.toJSON(result));
    }

    @Override
    public void destroy() {
        logger.info(">>>>>>>>>>>MyFilter>destroy>>>>>>>>>>>>>>>>");
    }
}